package com.fwj.storm.sentence;

import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichBolt;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Tuple;
import org.apache.storm.tuple.Values;

import java.util.Map;

/**
 * @author 傅文江
 * <p>
 * 时间： 2017-11-07 14:30
 * <p>
 * 描述：
 * <p>
 * 版权： Copyright © 2017 新道科技股份有限公司
 **/
public class SplitSentenceBolt extends BaseRichBolt {
    private OutputCollector collector;

    @Override
    public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) {
        this.collector = collector;
    }

    @Override
    public void execute(Tuple input) {
        String sentence = input.getStringByField("sentence");
        String[] split = sentence.split(" ");
        for (String word : split) {
            //将输出的tuple和输入的tuple锚定
            this.collector.emit(input, new Values(word));
        }
        //应答确认输入的tuple
        this.collector.ack(input);
    }

    @Override
    public void cleanup() {
        System.err.println("*******************执行了SplitSentenceBoltCleanUp 方法");
    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer declarer) {
        declarer.declare(new Fields("word"));
    }
}
